angular.module('review', [])
    .controller('reviewCtrl', ['$scope', 'httpService', 'alertmsg', '$rootScope', '$location', function($scope, httpService, alertmsg, $rootScope, $location) {
        $scope.type = 1;
        var pagenum = 1;
        // 初始化导出的参数
        $scope.searchData = {
            histroy: null,
            q: '',
            appoint: true,
            start_time: null,
            end_time: null,
            limit: limit
        };
        $scope.daoDate=null;
        if ($location.search() && $location.search().type) {
            $scope.type = $location.search().type;
            if($scope.type == 2) {
                $scope.searchData.histroy = true;
            }
            if($scope.type == 3) {
                $scope.searchData.type = 'confirm';
            }
        }

        $scope.setType = function(index) {
            $scope.type = index;
            pagenum = 1;
            $location.search({ type: index });
            $scope.handlenext(index);
            $scope.searchData['appoint'] = true;
//          console.log($scope.searchData.pay)
            delete $scope.searchData.pay;
            if(index == 1 && user.user_type == 'finance') {
                delete $scope.searchData.appoint;
                $scope.searchData['pay'] = true;
            }
        };

        // $scope.searchData = {
        //   q: $scope.q || '',
        //   start_time: $scope.startTime,
        //   end_time: $scope.endTime,
        // }


        // 获取订单：   待支付订单，待确认订单，历史订单
        var urls = {
            1: 'web/orders',
            2: 'web/orders',
//          2: 'web/orders/histroy_order',
            3: 'web/orders',
        };

        // console.log('user', user)
        $scope.getOrders = function(page, num) {
            // console.log('page, num, type', page, num, type)
            if(!!page) { pagenum = page }

            var params = {
                limit: limit,
                page: pagenum
            };

            if(num == 1) {
                if(user.user_type == 'finance') {
                    urls[1] = 'web/orders/pending_payments';
                    delete $scope.searchData.appoint;
                    $scope.searchData['pay'] = true;
                } else {
                    params['type'] = 'appoint';
                }
            }
            if(num==4){
            		if(user.user_type == 'manager'){
            			urls[1] = 'web/orders/pending_payments';
            			delete $scope.searchData.appoint;
            			$scope.searchData['pay'] = true;
            			num=1;
            		}
            }
			if(num==2){
				params['type'] = 'all';
			}
            if(num == 3) {
                params['type'] = 'confirm';
            }
            $scope.daoDate=angular.copy(params);
            $scope.daoDate.limit=$scope.searchData.limit;
            $scope.daoDate.url=urls[num];
           	delete $scope.daoDate.page;
            console.log(urls[num],params)
            httpService.query(urls[num], params,'get').then(function(res) {
                if(res.code == 200) {
                		console.log(res);
                    $scope.appointOrders = res.orders;
                    $scope.searchData.limit = res.page.total_count || 10000;
                    $scope.page = res.page;
                } else {
                    $scope.appointOrders = {};
                    $scope.searchData.limit = 10000;
                    $scope.page = {};
                    alertmsg(res.msg);
                }
            });

        };

        // 获取审核订单
        var type = 'appoint';
        $scope.getAppointOrders = function(page) {
            if (page) {
                pagenum = page;
            }
            // if ($rootScope.mobile) {
            //     type = 'all';
            // }
            var params = {
                page: pagenum,
                limit: limit
            };
            var url = "web/orders/pending_payments";
            httpService.query(url, params).then(function(res) {
                if (res.code == 200) {
                    $scope.appointOrders = res.orders;
                    console.log(res)
                    $scope.searchData.limit = res.page.total_count || 10000;
                    $scope.page = res.page;
                }
            })
        };
        // $scope.getAppointOrders(1);

        // 获取审核历史订单
        $scope.getHostroyOrder = function(page) {
            if (page) {
                pagenum = page;
            }
            var params = {
                page: pagenum,
                limit: limit
            };
            var url = "web/orders/histroy_order";
            httpService.query(url, params).then(function(res) {
                if (res.code == 200) {
                    $scope.appointOrders = res.orders;
                    console.log(res)
                    $scope.searchData.limit = res.page.total_count || 10000;
                    $scope.page = res.page;
                } else {
                    alertmsg(res.msg);
                }
            });
        };

        // 获取待确认订单
        $scope.getWaitOrder = function(page) {
            if (page) {
                pagenum = page;
            }
            httpService.query("web/orders", { page: pagenum, limit: limit, type: 'confirm' }).then(function(res) {
                if (res.code == 200) {
                    $scope.appointOrders = res.orders;
                    console.log(res)
                    $scope.searchData.limit = res.page.total_count || 10000;
                    $scope.page = res.page;
                } else {
                    alertmsg(res.msg);
                }
            });
        };

        // 下一页
        $scope.isSearch = false;
        $scope.getNext = function(page) {
            // if ($scope.type == 1) {
            //     $location.search({ type: $scope.type, page: page })
            //     $scope.getAppointOrders(page);
            // } else if ($scope.type == 2) {
            //     $location.search({ type: $scope.type, historypage: page })
            //     $scope.getHostroyOrder(page);
            // } else if ($scope.type == 3) {
            //     $location.search({ type: $scope.type, waitpage: page })
            //     $scope.getWaitOrder(page);
            // }

            $location.search({ type: $scope.type, page: page });
            $scope.getOrders(page, $scope.type);
        };

        $scope.goMap = function(role, id) {
            if (role == 2) {
                $state.go('service-maporders', { id: id });
            }
        };

        $scope.handlenext = function(type) {
            if ($location.search() && $location.search().page) {
                pagenum = $location.search().page
            }
            $scope.getOrders(pagenum, type);
            // if (type == 1) {
            //     if ($location.search() && $location.search().page) {
            //         pagenum = $location.search().page
            //     }
            //     $scope.getAppointOrders(pagenum);
            // } else if (type == 2) {
            //     if ($location.search() && $location.search().historypage) {
            //         pagenum = $location.search().historypage
            //     }
            //     $scope.getHostroyOrder(pagenum);
            // } else if (type == 3 ) {
            //     if ($location.search() && $location.search().waitpage) {
            //         pagenum = $location.search().waitpage
            //     }
            //     $scope.getWaitOrder(pagenum);
            // }
        };

        $scope.handlenext($scope.type)


        // 搜索功能
        // $scope.q 序列号, 或服务商名称, 省市区
        // $scope.appoint 是否查询审核订单
        // $scope.pay 是否查询待支付订单
        // $scope.histroy 是否查询历史订单
        // $scope.state 查询订单状态
        // $scope.startTime 时间戳, 到秒
        // $scope.endTime 时间戳, 秒
        // $scope.page 分页
        // $scope.limit 限制分页
        // 开始时间的初始化
        $scope.q,
        $scope.startTime,
        $scope.endTime,
        $scope.histroy;
        // 切换历史订单和（进行中/待审核）的订单
        $scope.setHistroy = function(bool, idx) {
            $scope.histroy = bool;
            $scope.isSearch = false;
            delete $scope.searchData.type;
            if (!$scope.histroy) {
              $scope.searchData.histroy = bool
            } else {
              $scope.searchData.histroy = null
            }
            if(idx == 3) {
                $scope.searchData.type = 'confirm';
            }
        };
        var dateOpts = {
            enableTime: true,
            dateFormat: 'Y-m-d H:i',
            mode: 'single',
            locale: 'zh'
        };
        function difDateOpts(isStart) {
            if (isStart) {
                dateOpts.onChange = function(selectedDates, dateStr, instance) {
                    $scope.startTime = new Date(selectedDates[0]).getTime() / 1000;
                    $('.endTime').attr('disabled', false);
                    flatpickr(".endTime", difDateOpts(false));
                }
            } else {
                dateOpts.minDate = $scope.startTime * 1000;
                dateOpts.onChange = function(selectedDates, dateStr, instance) {
                    $scope.endTime = new Date(selectedDates[0]).getTime() / 1000;
                    console.log($scope.endTime)
                }
            }
            return dateOpts;
        }
        $.getScript('./lib/flatpickr/dist/l10n/zh.js', function() {
            flatpickr(".startTime", difDateOpts(true));
        });
        $scope.searchOrder = function(page) {
            $scope.isSearch = true;

            // $scope.searchData = {
            //     q: $scope.q || '',
            //     appoint: true,
            //     start_time: $scope.startTime,
            //     end_time: $scope.endTime,
            //     page: parseInt(page, 10) || 0,
            //     limit: limit
            // };
            $scope.searchData.q = $scope.q;
            $scope.searchData.start_time = $scope.startTime;
            $scope.searchData.end_time = $scope.endTime;
            $scope.searchData.page = parseInt(page, 10) || 0;
            if($scope.type == 1 && user.user_type == 'finance') {
                $scope.searchData['pay'] = true;
                delete $scope.searchData.appoint;
            }
			if($scope.type==1 || $scope.type==3){
				 $scope.searchData.histroy=null;
			}
            if ($scope.histroy) {
                $scope.searchData.histroy = $scope.histroy;
            }

            if (isMobile) {
                $scope.searchData.type = 'all'
            }
			console.log($scope.searchData);
            httpService.query('web/search_orders', $scope.searchData).then(function(res) {
                if (res.code === 200) {
                    $scope.appointOrders = res.orders;
                    $scope.page = res.page;
                    $scope.searchData.limit = res.page.total_count;
                } else {
                    alertmsg(res.msg);
                }
            })

            if ($scope.isSearch && !page) {
              $('#search-modal').modal('toggle')
            }
        }

        

    }]);
